﻿namespace Smart.Utils.ExcelDataReader.Core
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;

    internal class XlsRootDirectory
    {
        private List<XlsDirectoryEntry> m_entries;
        private XlsHeader m_hdr;
        private XlsDirectoryEntry m_root;

        public XlsRootDirectory(XlsHeader hdr)
        {
            int num;
            this.m_root = null;
            this.m_hdr = hdr;
            byte[] src = new XlsStream(hdr, hdr.RootDirectoryEntryStart).ReadStream();
            List<XlsDirectoryEntry> list = new List<XlsDirectoryEntry>();
            for (num = 0; num < src.Length; num += 0x80)
            {
                byte[] dst = new byte[0x80];
                Buffer.BlockCopy(src, num, dst, 0, dst.Length);
                list.Add(new XlsDirectoryEntry(dst));
            }
            this.m_entries = list;
            for (num = 0; num < list.Count; num++)
            {
                XlsDirectoryEntry entry = list[num];
                if ((this.m_root == null) && (entry.EntryType == STGTY.STGTY_ROOT))
                {
                    this.m_root = entry;
                }
                if (entry.ChildSid != uint.MaxValue)
                {
                    entry.Child = list[(int) entry.ChildSid];
                }
                if (entry.LeftSiblingSid != uint.MaxValue)
                {
                    entry.LeftSibling = list[(int) entry.LeftSiblingSid];
                }
                if (entry.RightSiblingSid != uint.MaxValue)
                {
                    entry.RightSibling = list[(int) entry.RightSiblingSid];
                }
            }
        }

        public XlsDirectoryEntry FindEntry(string EntryName)
        {
            foreach (XlsDirectoryEntry entry in this.m_entries)
            {
                if (entry.EntryName == EntryName)
                {
                    return entry;
                }
            }
            return null;
        }

        public ReadOnlyCollection<XlsDirectoryEntry> Entries
        {
            get
            {
                return this.m_entries.AsReadOnly();
            }
        }

        public XlsDirectoryEntry RootEntry
        {
            get
            {
                return this.m_root;
            }
        }
    }
}

